package acwing;

import java.io.*;

public class A3 {
    static int n;
    static int m;
    static int N=1010;
    static int[] v=new int[N];
    static int[] w=new int[N];
    static int[] dp=new int[N];
    static BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
    static BufferedWriter log=new BufferedWriter(new OutputStreamWriter(System.out));
    public static void main(String[] args) throws IOException {
        String[] s = read.readLine().split(" ");
        n=Integer.parseInt(s[0]);
        m=Integer.parseInt(s[1]);
        for(int i=1;i<=m;i++){
            String[] s1 = read.readLine().split(" ");
            v[i]=Integer.parseInt(s1[0]);
            w[i]=Integer.parseInt(s1[1]);
        }
        //对第i个物品进行决策
        for(int i=1;i<=n;i++){
            for(int j=v[i];j<=m;j++)
                dp[j]=Math.max(dp[j],dp[j-v[i]]+w[i]);
        }
        //输出dp[m]
        log.write(Integer.toString(dp[m]));
        log.flush();
    }


}
